//
//  XiaoBianTuiJianTableViewCell.m
//  喜马拉雅FM
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 lanou.com. All rights reserved.


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "XiaoBianTuiJianTableViewCell.h"

#import "ModelForData.h"
#import "UIImageView+WebCache.h"
#import "SecendViewController.h"

#import "ModelForListen.h"

@interface XiaoBianTuiJianTableViewCell ()

@property (nonatomic, retain) UILabel *labelForSubTitle;
@property (nonatomic, retain) UIImageView *pic;
@property (nonatomic, retain) NSMutableArray *arrForImages;
@property (nonatomic, retain) NSMutableArray *arrForLabelSubTitle;
@property (nonatomic, retain) NSMutableArray *arrForAlbumID;

@property (nonatomic, retain) NSMutableArray *arrForListenData;

@end

@implementation XiaoBianTuiJianTableViewCell



- (void)dealloc {
    [_labelForSubTitle release];
    [_pic release];
    [_arrForImages release];
    [_arrForLabelSubTitle release];
    [_arrForAlbumID release];
    [_arrForListenData release];
    [super dealloc];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.arrForImages = [NSMutableArray array];
        self.arrForLabelSubTitle = [NSMutableArray array];
        self.arrForAlbumID = [NSMutableArray array];
        self.arrForListenData = [NSMutableArray array];
        
        [self createSubViews];
        [self createTapGestureRecognizer];
        
    }
    return self;
}

- (void)createSubViews {
    
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"biaoti"]];
    image.frame = CGRectMake(10, 15, 20, 20);
    [self.contentView addSubview:image];
    [image release];
    
    UILabel *labelForTitle = [[UILabel alloc]initWithFrame:CGRectMake(30, 15, 100,20)];
    [self.contentView addSubview:labelForTitle];
    labelForTitle.text = @"小编推荐";
    [labelForTitle release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(WIDTH - 100, 15, 100, 20);
    [button setTitle:@"更多 >" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
    
    
    for (int i = 0; i < 3; i++) {
        self.pic = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
        self.pic.userInteractionEnabled = YES;
        self.pic.frame = CGRectMake((WIDTH - 40) / 3 * i + 10 * (i + 1), 60, (WIDTH - 40) / 3, (WIDTH - 40) / 3 *4/3);
        [self.contentView addSubview:self.pic];
        self.pic.tag = 1 + i;
        [self.pic release];
        
        
        self.labelForSubTitle = [[UILabel alloc]initWithFrame:CGRectMake(((WIDTH - 40) / 3 + 10) * i + 10 , (WIDTH - 40) / 3 *4/3 + 60, (WIDTH - 40) / 3, 50)];
        self.labelForSubTitle.text = @"李力刚演讲";
        self.labelForSubTitle.numberOfLines = 0;
        self.labelForSubTitle.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.labelForSubTitle];
        self.labelForSubTitle.tag = 10 + i;
        [_labelForSubTitle release];
    }
}

- (void)handleAction:(UIButton *)button {
    
    NSLog(@"更多");
    NSNotification *notice =[NSNotification notificationWithName:@"XiaoBianTuiJian" object:nil];
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    
}

- (void)createTapGestureRecognizer {
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapAction:)];
    [[self.contentView viewWithTag:1] addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapAction:)];
    [[self.contentView viewWithTag:2] addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapAction:)];
    [[self.contentView viewWithTag:3] addGestureRecognizer:tap2];
    [tap2 release];
    [tap1 release];
    [tap release];
}

- (void)handleTapAction:(UITapGestureRecognizer *)tap {
    
    SecendViewController *secend = [[SecendViewController alloc]init];
    // 根据tap在哪个视图上来判断传值.
    if (tap.view == [self.contentView viewWithTag:1]) {
       
        secend.stringForAlbumID = self.arrForAlbumID[0];
        secend.modelForListen = self.arrForListenData[0];  
    
   
      
    }else if (tap.view == [self.contentView viewWithTag:2]){
        secend.stringForAlbumID = self.arrForAlbumID[1];
        secend.modelForListen = self.arrForListenData[1];
    }
    else {
        secend.stringForAlbumID = self.arrForAlbumID[2];
        secend.modelForListen = self.arrForListenData[2];
    }
    
    // 获取该视图层最下边的VC.
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    
    UIViewController *vc = (UIViewController*)object;
    [vc.navigationController pushViewController:secend animated:YES];
}

- (void)passModelForData:(ModelForData *)model {
 
    [self.arrForListenData removeAllObjects];
    NSArray *arr = [NSArray arrayWithArray:model.arrForXiaoBian];
    for (NSDictionary *dic in arr) {
        ModelForListen *modelForListen = [[ModelForListen alloc]init];
        [modelForListen setValuesForKeysWithDictionary:dic];
        [self.arrForListenData addObject:modelForListen];
    }
    
    [self.arrForAlbumID removeAllObjects];
    [self.arrForImages removeAllObjects];
    [self.arrForLabelSubTitle removeAllObjects];
    
    for (NSDictionary *dic in model.arrForXiaoBian) {
        
        NSString *str = [dic objectForKey:@"coverLarge"];
        [self.arrForImages addObject:str];
        str = [dic objectForKey:@"trackTitle"];
        [self.arrForLabelSubTitle addObject:str];
        str = [dic objectForKey:@"albumId"];
        [self.arrForAlbumID addObject:str];
        
    }
    for (int i = 0; i < 3; i++) {
        UIImageView *image = [self.contentView viewWithTag:(1 + i)];
        [image sd_setImageWithURL:[NSURL URLWithString:self.arrForImages[i]]];
        
        UILabel *label = [self.contentView viewWithTag:(10 + i)];
        label.text = self.arrForLabelSubTitle[i];
    }
}

- (void)awakeFromNib {

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
